// pages/address/address.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address: '',
        name: "",
        phone: "",
        show: true,
        xiushow:true,
        index:"",
        Default:"",
        addressGroup: [{
                name: "张三",
                phone: "123123123123",
                address: "广东省广州市天河区珠吉路999号",
            },
            {
                name: "李四",
                phone: "213412341243",
                address: "广东省广州市天河区珠吉路888号",
            }, {
                name: "王五",
                phone: '123123123123',
                address: "广东省广州市天河区珠吉路777号",
            },
        ],
    },
    name(e) {
        this.setData({
            name: e.detail.value
        })

    },
    phone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    address(e) {
        this.setData({
            address: e.detail.value
        })
    },
    add() {
        this.setData({
            show: !this.data.show,
            xiushow:true,
        })
    },
    del(options){
        let addressGroup = wx.getStorageSync('addressGroup');
        addressGroup.splice(options.target.dataset.index,1)
                    wx.setStorageSync('addressGroup', addressGroup);
                    this.setData({
                        addressGroup
                    })
    },
    xiuDefault(options){
        this.setData({
            Default:options.target.dataset.index,
        })
        wx.setStorageSync('Default', options.target.dataset.index)
    },
    confirm(){
        this.add();
        this.setData({
            name: "",
            phone: "",
            address: "",
        })
    },
    xiu(options){
      let information=this.data.addressGroup[options.target.dataset.index];
      console.log(information);
      this.add();
      this.setData({
        index:options.target.dataset.index,
        xiushow:false,
        name: information.name,
        phone: information.phone,
        address: information.address,
    })
    },
    xiugai(){
        let addressGroup = wx.getStorageSync('addressGroup');
        wx.chooseLocation({
            success: (data) => {
                this.setData({
                    address: data.address
                })
                let addressGroup = wx.getStorageSync('addressGroup');
                if (this.data.name !== "" && this.data.phone !== "") {
                    addressGroup.splice(this.data.index,1,{
                        name: this.data.name,
                        phone: this.data.phone,
                        address: this.data.address,
                    })
                    wx.setStorageSync('addressGroup', addressGroup);
                    this.setData({
                        addressGroup,
                        xiushow:true,
                    })
                }
            },
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {},
    getAddress() {
        wx.chooseLocation({
            success: (data) => {
                this.setData({
                    address: data.address
                })
                let addressGroup = wx.getStorageSync('addressGroup');
                if (this.data.name !== "" && this.data.phone !== "") {
                    addressGroup.push({
                        name: this.data.name,
                        phone: this.data.phone,
                        address: this.data.address,
                    })
                    wx.setStorageSync('addressGroup', addressGroup);
                    this.setData({
                        addressGroup,
                    })
                }
            },
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            Default: wx.getStorageSync('Default'),
            addressGroup: wx.getStorageSync('addressGroup'),
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})